#!/bin/sh
# Set settings, make a camera take photos.

# TODO
# - In filenames, save setting values instead of option numbers.
# - Take shots with incrimenting/decrimenting values over a range.
# - Timed shots.

cimg="capt0000.jpg"	# name of file from camera

usage="Usage: $0 i # a # s # w #
Any or all paramaters can be excluded, you will be prompted \
with a list of options. All values are numerical options \
offered by the camera, not the desired value."

while [ $# -gt 0 ]
do
	case "$1" in
		i*) i="$2"; shift ;;	# iso
		a*) a="$2"; shift ;;	# aperture
		s*) s="$2"; shift ;;	# shutter speed
		w*) w="$2"; shift ;;	# white balance
		*) echo "$usage"; exit ;;
	esac
	shift
done

precap() { 
	if [ -z "$i" ]; then			# check if option is set
		gphoto2 --get-config "iso" 	# get options from camera
		printf '? '			# prompt for input
		read i
	fi

	if [ -z "$a" ]; then
		gphoto2 --get-config "aperture" 
		printf '? '
		read a
	fi

	if [ -z "$s" ]; then
		gphoto2 --get-config "shutterspeed" 
		printf '? '
		read s
	fi

	if [ -z "$w" ]; then
		gphoto2 --get-config "whitebalance" 
		printf '? '
		read w
	fi
}

cap() {
	# display settings and pause
	echo "
     ISO: $i
 Shutter: $s
Aperture: $a

Press [ENTER] to take photo:"
	read opt

	# note time and settings used
	oimg="$(date +%y%m%d-%H%M%S)-i${i}-a${a}-s${s}.jpg"

	# set options
	gphoto2 --set-config "iso=$i"
	gphoto2 --set-config "whitebalance=$w"
	gphoto2 --set-config "aperture=$a"
	gphoto2 --set-config "shutterspeed=$s"

	# turn the screen off
	xrandr --output LVDS --off
	sleep 1

	# capture the image
	echo "Taking photo..."
	gphoto2 --capture-image-and-download
}

postcap() {
	# turn the screen back on
	xrandr --output LVDS --auto --below HDMI-0

	# rename the image
	mv "$cimg" "$oimg"

	# display the image
	feh -.B black "$oimg"

	# prompt and delete the image
	printf 'Keep image? '
	read opt
	case "$opt" in
		n) rm -f "$oimg" ;;
		y) ;;
	esac
}

precap	# gather information before capturing image
cap	# set options and capture image
postcap	# rename, display and give option to discard

